﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.YouTube;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using TuoiThoProject.DAL;

namespace TuoiThoProject.BLL
{
    public class VideoBLL
    {
        NewsDataContext data = new NewsDataContext();

        public List<VideoEntity> getVideo_All(string GmailId, int width, int height, int frameBorder)
        {
            tblPicasa user = data.tblPicasas.SingleOrDefault(c => c.cGmailId == GmailId);
            List<VideoEntity> lstVideo = new List<VideoEntity>();
            if (user != null)
            {
                YouTubeRequestSettings settings = new YouTubeRequestSettings("Ibeta youtube",
                    "AI39si58YeNbUhG_hcAFN5ZXiEtBDcWmw5tVmndpi2PFTt2AyYrQr0b38KSl6TiWQ10OSltIlzStAuIkH4Vv98GC2dsD1tbAEg",
                    user.cGmailId,
                    user.cPassword);
                YouTubeRequest request = new YouTubeRequest(settings);

                Feed<Video> feedVideo = request.Get<Video>(new Uri("http://gdata.youtube.com/feeds/api/users/default/uploads"));
                int count = feedVideo.Entries.Count();
                foreach (Video v in feedVideo.Entries)
                {
                    VideoEntity vEntity = new VideoEntity
                    {
                        Title = v.Title,
                        Description = v.Description,
                        IsDraft = v.IsDraft,
                        //State = v.Status.Name,
                        ReadOnly = v.ReadOnly,
                        RatingAverage = v.RatingAverage,
                        ViewCount = v.ViewCount,
                        Thumbnail_Url = v.Thumbnails[0].Url,
                        Thumbnail_Time = v.Thumbnails[0].Time,
                        MediaContent_Url = v.Contents[0].Url,
                        MediaContent_Format = v.Contents[0].Format,
                        MediaContent_Duration = v.Contents[0].Duration,
                        Embed = String.Format("<iframe id='{4}' type='text/html' width='{0}' height='{1}' src='http://www.youtube.com/embed/{2}' frameborder='{3}'></iframe>", width, height, v.VideoId, frameBorder, v.VideoId)
                    };

                    lstVideo.Add(vEntity);
                }


            }
            return lstVideo;
        }

        public List<VideoEntity> getVideo_VideoId(string VideoId, string GmailId, int width, int height, int frameBorder)
        {
            tblPicasa user = data.tblPicasas.SingleOrDefault(c => c.cGmailId == GmailId);
            List<VideoEntity> lstVideo = new List<VideoEntity>();
            if (user != null)
            {
                YouTubeRequestSettings settings = new YouTubeRequestSettings("Ibeta youtube",
                    "AI39si58YeNbUhG_hcAFN5ZXiEtBDcWmw5tVmndpi2PFTt2AyYrQr0b38KSl6TiWQ10OSltIlzStAuIkH4Vv98GC2dsD1tbAEg",
                    user.cGmailId,
                    user.cPassword);
                YouTubeRequest request = new YouTubeRequest(settings);

                Feed<Video> feedVideo = request.Get<Video>(new Uri("http://gdata.youtube.com/feeds/api/videos/" + VideoId));

                foreach (Video v in feedVideo.Entries)
                {
                    VideoEntity vEntity = new VideoEntity
                    {
                        Title = v.Title,
                        Description = v.Description,
                        IsDraft = v.IsDraft,
                        //State = v.Status.Name,
                        ReadOnly = v.ReadOnly,
                        RatingAverage = v.RatingAverage,
                        ViewCount = v.ViewCount,
                        Thumbnail_Url = v.Thumbnails[0].Url,
                        Thumbnail_Time = v.Thumbnails[0].Time,
                        MediaContent_Url = v.Contents[0].Url,
                        MediaContent_Format = v.Contents[0].Format,
                        MediaContent_Duration = v.Contents[0].Duration,
                        Embed = String.Format("<iframe id='ytplayer' type='text/html' width='{0}' height='{1}' src='http://www.youtube.com/embed/{2}' frameborder='{3}'/>", width, height, v.VideoId, frameBorder)
                    };

                    lstVideo.Add(vEntity);
                }


            }
            return lstVideo;
        }

        public void UploadVideo(string GmailId, string Title, string Description, bool isPublic, string path, ESupportType fileType)
        {
            tblPicasa user = data.tblPicasas.SingleOrDefault(c => c.cGmailId == GmailId);

            if (user != null)
            {
                YouTubeRequestSettings settings = new YouTubeRequestSettings("Ibeta youtube",
                              "AI39si58YeNbUhG_hcAFN5ZXiEtBDcWmw5tVmndpi2PFTt2AyYrQr0b38KSl6TiWQ10OSltIlzStAuIkH4Vv98GC2dsD1tbAEg",
                              user.cGmailId,
                              user.cPassword);
                YouTubeRequest request = new YouTubeRequest(settings);

                Video newVideo = new Video { Title = Title, Description = Description };
                newVideo.YouTubeEntry.Private = isPublic;

                switch (fileType)
                {
                    case ESupportType.WebM:
                        break;
                    case ESupportType.MPEG4:
                        break;
                    case ESupportType._3GPP:
                        break;
                    case ESupportType.MOV:
                        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(path, "video/quicktime");
                        break;
                    case ESupportType.AVI:
                        break;
                    case ESupportType.MPEGPS:
                        break;
                    case ESupportType.WMV:
                        break;
                    case ESupportType.FLV:
                        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(path, "video/x-flv");
                        break;
                    default:
                        break;
                }

                Video createdVideo = request.Upload(newVideo);
            }


        }
    }
}